/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.freshdirect.src.service;

import com.freshdirect.src.dao.ProductDAO;
import com.freshdirect.src.model.Product;
import com.freshdirect.src.model.ShoppingCart;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Ilias
 */
public class ShoppingCartService {

    public void addProduct(int id, int n, ShoppingCart cart) {
         //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
         
        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
        System.out.println("ShoppingCartService -> addProduct");
        
        int flag = 0;
         for(int i=0; i<cart.getProducts().size(); i++){
            System.out.println("Match product: "+ i);
            if(cart.getProducts().get(i).getId()==id){
                System.out.println("There is product!");
                flag = 1;
                cart.getProducts().get(i).setQuantity(cart.getProducts().get(i).getQuantity()+n);
                cart.increaseTotalPrice(cart.getProducts().get(i).getPrice()*n);
            }
        }
        if(flag==0){
            System.out.println("First Product");
            productDAO.setDataSource("user");
            Product product = productDAO.getById(id);
            product.setQuantity(n);
            product.setProcurLevel(0);
            cart.getProducts().add(product);
            cart.increaseTotalPrice(product.getPrice()*n);
        }
        
    }
    
    public void removeProduct(int id, ShoppingCart cart) {
        
        int flag = 0;
         for(int i=0; i<cart.getProducts().size(); i++){
            System.out.println("Match product: "+ i);
            if(cart.getProducts().get(i).getId()==id){
                System.out.println("There is product!");
                flag = 1;
                cart.decreaseTotalPrice(cart.getProducts().get(i).getPrice()*cart.getProducts().get(i).getQuantity());
                cart.getProducts().remove(i);
                cart.setTotalPrice(0);
                for(int j=0; j<cart.getProducts().size(); j++){
                    cart.increaseTotalPrice(cart.getProducts().get(j).getQuantity() * cart.getProducts().get(j).getPrice());
                }
                break;
            }
        }
        if(flag==0){
            //MESSAGE
        }
        
    }
    
     public void updateProduct(int id, int n, ShoppingCart cart) {
        
        int flag = 0;
         for(int i=0; i<cart.getProducts().size(); i++){
            System.out.println("Match product: "+ i);
            if(cart.getProducts().get(i).getId()==id){
                System.out.println("There is product!");
                flag = 1;
                cart.getProducts().get(i).setQuantity(n);
                cart.setTotalPrice(0);
                for(int j=0; j<cart.getProducts().size(); j++){
                    cart.increaseTotalPrice(cart.getProducts().get(j).getQuantity() * cart.getProducts().get(j).getPrice());
                }
                break;
            }
        }
        if(flag==0){
            //MESSAGE
        }
        
    }

    public ShoppingCart checkAvailability(ShoppingCart cart) {
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        
        //Get the ProductDAO Bean
        ProductDAO productDAO = ctx.getBean("productDAO", ProductDAO.class);
        cart.setChange(0);
        for(int i = 0; i< cart.getProducts().size(); i++){
            cart.getProducts().get(i).setProcurLevel(0);
            productDAO.setDataSource("user");
            Product product = productDAO.getById(cart.getProducts().get(i).getId());
            cart.getProducts().get(i).setPrice(product.getPrice());
            if (product.getQuantity() != 0){
                if(cart.getProducts().get(i).getQuantity() > product.getQuantity()){
                    cart.getProducts().get(i).setQuantity(product.getQuantity());
                    cart.getProducts().get(i).setProcurLevel(1);
                    cart.setChange(1);
                }
            }
            else{
                cart.getProducts().remove(i);
                cart.setChange(1);
            }
        }
        double  previousPrice = cart.getTotalPrice();
        cart.setTotalPrice(0);
        for(int j=0; j<cart.getProducts().size(); j++){
            cart.increaseTotalPrice(cart.getProducts().get(j).getQuantity() * cart.getProducts().get(j).getPrice());
        }
        if(previousPrice != cart.getTotalPrice()){
            cart.setChange(1);
        }
        return cart;
    }
}
